package com.szholly.plug.activity.interceptor;

import java.util.ArrayList;
import java.util.List;

import com.szholly.utils.util.StringUtils;

/**
 * 定义流程处理(通过，不通过)通知事件
 */
public abstract class BpmHandleOptionBase implements IBpmHandleOption {

	private int index;
	private List<String> bpmId = new ArrayList<String>();
	private List<String> taskId = new ArrayList<String>();
	
	@Override
	public void setIndex(int index) {
		this.index = index;
	}

	@Override
	public int getIndex() {
		return this.index;
	}

	@Override
	public void addBpmID(String bpmId) {
		if(!StringUtils.IsNullOrSpace(bpmId)){
			this.bpmId.add(bpmId.toUpperCase());
		}
	}
	
	@Override
	public void removeBpmID(String bpmId) {
		if(!StringUtils.IsNullOrSpace(bpmId)){
			this.bpmId.remove(bpmId.toUpperCase());
		}
	}

	/**
	 * 是否包含流程ID
	 * @param bpmId
	 * @return
	 */
	@Override
	public boolean containsBpmID(String bpmId){
		if(this.bpmId==null ||
				this.bpmId.size()<=0){
			return true;
		}
		if(!StringUtils.IsNullOrSpace(bpmId)){
			return this.bpmId.contains(bpmId.toUpperCase());
		}
		return false;
	}
	
	@Override
	public void addTaskId(String taskId) {
		if(!StringUtils.IsNullOrSpace(taskId)){
			this.taskId.add(taskId.toUpperCase());
		}
	}
	
	@Override
	public void removeTaskId(String taskId) {
		if(!StringUtils.IsNullOrSpace(taskId)){
			this.taskId.remove(taskId.toUpperCase());
		}
	}
	
	/**
	 * 是否包含任务ID
	 * @param bpmId
	 * @return
	 */
	@Override
	public boolean containsTaskId(String taskid){
		if(this.taskId==null ||
				this.taskId.size()<=0){
			return true;
		}
		if(!StringUtils.IsNullOrSpace(taskid)){
			return this.taskId.contains(taskid.toUpperCase());
		}
		return false;
	}

	@Override
	public abstract void taskOption(OptionInfo optionInfo);
	
	/**
	 * 编辑操作前
	 */
	public static void editOption(OptionInfo optionInfo) {
		for (IBpmHandleOption editItem : BpmInterceptorRegister.bpmHandleOption) {
			if(editItem!=null
					&& editItem.containsBpmID(optionInfo.BpmId)
					&& (editItem.containsTaskId(optionInfo.TaskDefinitionKey)  || editItem.containsTaskId(optionInfo.TaskPDefinitionKey))){
				editItem.taskOption(optionInfo);
			}
		}
	}
}
